<?php


namespace app\api\service;

use app\common\cache\SmsVerifyCache;
use app\common\model\user\User;
use think\image\Exception;
use tmcore\base\BaseService;
use app\common\service\UserService as ComUserService;
use tmcore\exceptions\ValidateException;

class UserService extends BaseService
{
    /**
     * 获取个人中心信息
     */
    public function getCenter() {

    }

    /**
     * 获取个人信息
     * @param $uid
     * @return array
     */
    public static function getInfo($uid) {
        if(!$uid) return [];
        $user = User::where(['id' => $uid])->findOrEmpty();
        if($user) {
            return [
                'user_token' => $user['user_token'],
                'group_id' => $user['group_id'],
                'account' => $user['account'],
                'nickname' => $user['nickname'],
                'real_name' => $user['real_name'],
                'birthday' => $user['birthday'],
                'id_card' => $user['id_card'],
                'avatar' => get_file_url($user['avatar']),
                'sex' => $user['sex'],
                'mobile' => $user['mobile'],
                'email' => $user['email'],
                'status' => $user['status'],
                'create_time' => $user['create_time'],
            ];
        } else {
            return [];
        }
    }

    /**
     * 修改密码
     * @param $uid
     * @param $password
     * @return bool
     * @throws ValidateException
     */
    public static function updatePassword($uid, $password) {
        (new ComUserService)->updateInfo('update_password', ['id' => $uid, 'password' => $password]);
        return true;
    }

    /**
     * 修改手机号
     * @param $uid
     * @param $mobile
     * @param $new_mobile
     * @param $code
     * @return bool
     * @throws ValidateException
     */
    public static function updateMobile($uid, $mobile ,$new_mobile, $code) {
        $SmsVerifyCache = (new SmsVerifyCache);
        $scene = $mobile ? 'change_mobile' : 'bind_mobile';
        $SmsVerifyCache->verifySmsCode($new_mobile, $scene, $code);
        (new ComUserService)->updateInfo('update_mobile', ['id' => $uid, 'mobile' => $new_mobile]);
        $SmsVerifyCache->deleteSmsCode($new_mobile, $scene);

        return true;
    }

    /**
     * 修改头像
     * @param $uid
     * @param $avatar
     * @return bool
     * @throws ValidateException
     */
    public static function updateAvatar($uid, $avatar) {
        (new ComUserService)->updateInfo('update_info', ['id' => $uid, 'avatar' => $avatar]);
        return true;
    }

    /**
     * 修改昵称
     * @param $uid
     * @param $name
     * @return bool
     * @throws ValidateException
     */
    public static function updateNickname($uid, $name) {
        (new ComUserService)->updateInfo('update_info', ['id' => $uid, 'nickname' => $name]);
        return true;
    }

    /**
     * 修改邮箱
     * @param $uid
     * @param $email
     * @return bool
     * @throws ValidateException
     */
    public static function updateEmail($uid, $email) {
        (new ComUserService)->updateInfo('update_email', ['id' => $uid, 'email' => $email]);
        return true;
    }

    /**
     * 修改性别
     * @param $uid
     * @param $sex
     * @return bool
     * @throws ValidateException
     */
    public static function updateSex($uid, $sex) {
        (new ComUserService)->updateInfo('update_info', ['id' => $uid, 'sex' => $sex]);
        return true;
    }

    /**
     * 通过小程序获取手机号
     */
    public function getMobileByApplet() {

    }

    /**
     * 修复微信授权
     */
    public function checkWechatAuth() {

    }
}